package com.acme.bankapp.service.bank;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.Scanner;

import com.acme.bankapp.domain.bank.AbstractAccount;
import com.acme.bankapp.domain.bank.Account;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.CheckingAccount;
import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.domain.bank.Gender;
import com.acme.bankapp.domain.bank.SavingAccount;
import com.acme.bankapp.domain.exceptions.ClientExistsException;

public class BankDataLoaderService extends BankService {

	private FileReader fr;
	private LineNumberReader lnr;

	public BankDataLoaderService(String fP, String saveReadFile) throws FileNotFoundException {
		super(fP, saveReadFile);		
				
		fr = new FileReader(fP);
		lnr = new LineNumberReader(fr);			
	}
	
	public void bankLoad(Bank bank) throws IOException {
		try {
			String clientStr;
			Client client;
			while ((clientStr = lnr.readLine()) != null) {
				client = feedParcer(clientStr, lnr.getLineNumber());
				try {
					addClient(bank, client);					
				} catch (ClientExistsException e) {
					System.out.println("Client " + client + "is already exists");
				}
			}
		} 
		finally {
			lnr.close();
			fr.close();	
		}
		bankLoading = false;
	}
	
	public Client feedParcer (String scanMe, int id) throws IOException {
		String at;
		String name;
		Gender gender;
		String g;
		double balance;
		double overdraft;
		AbstractAccount account;
		Scanner parcer = new Scanner(scanMe);
		parcer.useDelimiter(";");
				
		try{			
				at = scannerLoader(parcer, "accounttype");	
				
				if((!at.equals("c")) && (!at.equals("s"))) {
					throw new IOException();
				}
				
				try {
					balance = Double.parseDouble(scannerLoader(parcer, "balance"));
					overdraft = Double.parseDouble(scannerLoader(parcer, "overdraft"));
				}
				catch (NumberFormatException e) {
					throw new IOException();
				}
			
				switch (at) {
					case "s": {
						account = new SavingAccount(0.0, id);
						break;}
					case "c": {
						account = new CheckingAccount(0.0, id, overdraft);
						break;}
					default: throw new IOException();					
				}
				name = scannerLoader(parcer, "name");
				g = scannerLoader(parcer, "gender");
				
				switch (g) {
					case "m": {
						gender = Gender.MALE;
						break;}
					case "f": {
						gender = Gender.FEMALE;
						break;}
					default: throw new IOException();					
				}	
				
				Client client = new Client(account, balance, name, gender);
				return client;
				
		}
		catch (IOException e) {
			parcer.close();
			System.out.println("wrong input in feedFile");
			throw new IOException();
		}
	}

	private String scannerLoader(Scanner parcer, String etalon) throws IOException { 
		if (!parcer.hasNext()) {				
			throw new IOException();
		}
		String resultStr;
		Scanner s = new Scanner(parcer.next());
		s.useDelimiter("=");
		if(!s.hasNext()) {	
			s.close();
			throw new IOException();
		}
		if (!s.next().equals(etalon)) {
			s.close();
			throw new IOException();
		}		
		resultStr = s.next();
		s.close();
		return resultStr;
	}
	
}
